<?php
namespace app\common\logic;

use think\Controller;
use app\common\model\Collection as CollectionModel;

class Collection extends Controller
{

    /**
     * @param $shopid
     * @param int $start
     * @param int $num
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取店铺的收藏列表
     */
    public function getCollectionListByShopid($shopid,$start=1,$num=20){

       return CollectionModel::alias('c')
           ->join('t_shop s','c.shopid = s.id','LEFT')
           ->join('t_picture p','s.thumb = p.id','LEFT')
           ->join('t_area a','s.area_id= a.ID','LEFT')
           ->where("c.shopid",$shopid)
           ->order("c.addtime desc")
           ->page($start,$num)->select();

    }

    /**
     * @param $userid
     * @param int $start
     * @param int $num
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取用户的收藏列表
     */
    public function getCollectionListByUserid($userid,$start=1,$num=20){

        return CollectionModel::alias('c')
            ->join('t_shop s','c.shopid = s.id','LEFT')
            ->join('t_picture p','s.thumb = p.id','LEFT')
            ->join('t_area a','s.area_id= a.ID','LEFT')
            ->where("uid",$userid)
            ->order("c.addtime desc")
            ->page($start,$num)->select();

    }

    /**
     * @param $shopid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取店铺的收藏数量
     */
    public function getCollectionListByShopidCount($shopid){

        return CollectionModel::where("shopid",$shopid)->count();

    }


    /**
     * @param int $userid
     * @param int $shopid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 收藏
     */
    public function user_collect($userid,$shopid){

       $isuser=$this->is_user_collection($userid,$shopid);

       if($isuser){

           CollectionModel::where("uid",$userid)->where("shopid",$shopid)->delete();
           return false;

       }else{

           $collectdata=CollectionModel::create([
               "uid"=>$userid,
               "shopid"=>$shopid,
               "addtime"=>time()
           ]);
           return true;

       }



    }


    /**
     * @param int $userid
     * @param int $shopid
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 是否收藏
     */
    public function is_user_collection($userid,$shopid){

        $conllection_data = CollectionModel::where("uid",$userid)->where("shopid",$shopid)->find();
        if($conllection_data){
            return true;
        }else{
            return false;
        }
    }


}